package com.hengxinguotong.zhihuichengjian.ui;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.PopupWindow;
import android.widget.Switch;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXEditTextView;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.net.URI;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author wulei
 * @Date 2018/7/17
 */

public class ChangeIPActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.show_ip_tv)
    HXTextView showIPTv;
    @BindView(R.id.ip_etv)
    HXEditTextView IPEtv;
    @BindView(R.id.port_etv)
    HXEditTextView portEtv;
    @BindView(R.id.switch_bt)
    Switch switchBt;

    private String url = "http://ip:port/escst_interface";
    private String ip;
    private String port;
    private static final String URL_PREFIX = "http://";
    private static final String URL_SUFFIX = "/escst_interface";
    private PopupWindow pw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ip_change);
        ButterKnife.bind(this);
        initView();
    }

    private void initView(){
        url = Utils.getServerUrl(this);
        ip = getIP(url);
        port = getPort(url);
        titleTv.setText("服务器设置");
        showIPTv.setText(url);
        IPEtv.setText(ip);
        if(port.equals("-1")){
            port = "80";
        }
        portEtv.setText(port);
        if(url.contains(URL_SUFFIX)){
            switchBt.setChecked(true);
        }else{
            switchBt.setChecked(false);
        }
        IPEtv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ip = s.toString();
                url = URL_PREFIX + ip + ":" + port + URL_SUFFIX;
                showIPTv.setText(url);
            }
        });
        portEtv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                port = s.toString();
                if(port.equals("80")){
                    url = URL_PREFIX + ip + URL_SUFFIX;
                }else{
                    url = URL_PREFIX + ip + ":" + port + URL_SUFFIX;
                }
                showIPTv.setText(url);
            }
        });
    }

    @OnClick({R.id.select_iv, R.id.save_tv, R.id.back_tv})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back_tv:
                finish();
                break;
            case R.id.save_tv:
                Utils.setServerUrl(this, url);
                finish();
                break;
            case R.id.select_iv:
                showPopWindow(view);
                break;
            case R.id.tv_high:
                ip = "221.234.44.78";
                IPEtv.setText(ip);
                port = "81";
                portEtv.setText(port);
                url = URL_PREFIX + ip + ":" + port + URL_SUFFIX;
                showIPTv.setText(url);
                pw.dismiss();
                break;
            case R.id.tv_sub:
                ip = "42.51.69.36";
                IPEtv.setText(ip);
                port = "8080";
                portEtv.setText(port);
                url = URL_PREFIX + ip + ":" + port + URL_SUFFIX;
                showIPTv.setText(url);
                pw.dismiss();
                break;
            case R.id.tv_standard:
                ip = "58.49.51.35";
                IPEtv.setText(ip);
                port = "8899";
                portEtv.setText(port);
                url = URL_PREFIX + ip + ":" + port + URL_SUFFIX;
                showIPTv.setText(url);
                pw.dismiss();
                break;
        }
    }

    @OnCheckedChanged({R.id.switch_bt})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked && !url.contains(URL_SUFFIX)){
            url = url + URL_SUFFIX;
        }
        if(!isChecked && url.contains(URL_SUFFIX)){
            url = url.replace(URL_SUFFIX, "");
        }
        showIPTv.setText(url);
    }

    //弹出选择框
    private void showPopWindow(View view){
        View popView = LayoutInflater.from(this).inflate(R.layout.ip_popup_window, null, false);
        HXTextView tv_high = (HXTextView) popView.findViewById(R.id.tv_high);
        tv_high.setText("78");
        tv_high.setOnClickListener(this);
        HXTextView tv_sub = (HXTextView) popView.findViewById(R.id.tv_sub);
        tv_sub.setText("36");
        tv_sub.setOnClickListener(this);
        HXTextView tv_standard = (HXTextView) popView.findViewById(R.id.tv_standard);
        tv_standard.setText("35");
        tv_standard.setOnClickListener(this);
        // 创建PopupWindow对象，其中：
        // 第一个参数是用于PopupWindow中的View，第二个参数是PopupWindow的宽度，第三个参数是PopupWindow的高度，
        // 第四个参数指定PopupWindow能否获得焦点
        pw = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 设置PopupWindow的背景
        pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置PopupWindow是否能响应外部点击事件
        pw.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        pw.setTouchable(true);
        // 显示PopupWindow，其中：
        // 第一个参数是PopupWindow的锚点，第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
        pw.showAsDropDown(view, -30, 0);
        // 或者也可以调用此方法显示PopupWindow，其中：
        // 第一个参数是PopupWindow的父View，第二个参数是PopupWindow相对父View的位置，
        // 第三和第四个参数分别是PopupWindow相对父View的x、y偏移
        // popupWindow.showAtLocation(parent, gravity, x, y);
    }

    private static String getIP(String urlString) {
        Uri uri = Uri.parse(urlString);
        URI effectiveURI = null;
        String scheme = null,
                userInfo = null,
                host = null;
        int port;

        try {
            // URI(String scheme, String userInfo, String host, int port, String
            // path, String query,String fragment)
            effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
            scheme = uri.getScheme();
            userInfo = uri.getUserInfo();
            host = uri.getHost();
            port = uri.getPort();
            Log.d("", "getIP: " + scheme + userInfo + host + port);
        } catch (Throwable var4) {
            effectiveURI = null;
        }

        return host;
    }

    private static String getPort(String urlString) {
        Uri uri = Uri.parse(urlString);
        URI effectiveURI = null;
        String scheme = null,
                userInfo = null,
                host = null;
        int port = 80;

        try {
            // URI(String scheme, String userInfo, String host, int port, String
            // path, String query,String fragment)
            effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
            scheme = uri.getScheme();
            userInfo = uri.getUserInfo();
            host = uri.getHost();
            port = uri.getPort();
            Log.d("", "getIP: " + scheme + userInfo + host + port);
        } catch (Throwable var4) {
            effectiveURI = null;
        }

        return String.valueOf(port);
    }
}
